package com.chenshu.springmvc.controller;

import com.chenshu.springmvc.model.User;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletRequest;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-04-11
 * Time：11:07
 */
@RestController
@RequestMapping("user")//路由注册
public class UserController {
    @RequestMapping(value = "/hi", method = {RequestMethod.POST, RequestMethod.GET})//路由注册
//    @PostMapping("/hi")
//    @GetMapping("/hi")
    public String doController() {
        return "<h1> Hello Spring MVC !<h1>";
    }

    @RequestMapping("/getOneParam1")
    public String getOneParam1(HttpServletRequest request) {
        //return "Hi," + request.getParameter("name");
        Integer age = Integer.valueOf(request.getParameter("age"));
        return "value: " + age;
    }

    @RequestMapping("/getOneParam2")
    public String getOneParam2(Integer age) {
        //return "Hi," + name;
        return "value: " + age;
    }

    @RequestMapping("/getMoreParam")
    public String getMoreParam(String name, Integer age) {
        return "name: " + name + " | " + "age: " + age;
    }

    @RequestMapping("/getMoreParam2")
    public String getMoreParam(User user) {
        return user.toString();
    }

    @RequestMapping("/login")
    public String login(String name, String password) {
        return "name: " + name + " | " + "password: " + password;
    }

    @RequestMapping("/login2")
    public String loginAjax(@RequestBody User user) {
        return "name: " + user.getName() + " | " + "password：" + user.getPassword();
    }

    @RequestMapping("/upload")
    public String upload(@RequestPart("myfile")MultipartFile file, ServletRequest request) throws IOException {
        String path = "/Users/chenshu/Code/classcode_java/blog-demo/Spring-MVC/src/main/resources/static/img";
        file.transferTo(new File(path+"/cat.jpg"));
        return "success";
    }

    @RequestMapping("/header1")
    public String getHeader1(HttpServletRequest request) {
        String header = request.getHeader("User-Agent");
        return "User-Agent: " + header;
    }

    @RequestMapping("/header2")
    public String getHeader2(@RequestHeader("User-Agent") String header) {
        return "User-Agent: " + header;
    }

    @RequestMapping("/setCookie")
    public void setCookie(HttpServletResponse response) {
        Cookie cookie = new Cookie("status", "OK");
        response.addCookie(cookie);
    }
    @RequestMapping("/cookie1")
    public String getCookie1(HttpServletRequest request) {
        String ret = "status: ";
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("status")) {
                ret += cookie.getValue();
            }
        }
        return ret;
    }

    @RequestMapping("/cookie2")
    public String getCookie2(@CookieValue("status") String status) {
        return "status: " + status;
    }

    @RequestMapping("/setSession")
    public String setSession(HttpServletRequest request) {
        HttpSession session = request.getSession(true);
        if (session!=null) {
            session.setAttribute("username", "zhangsan");
        }
        return "session 存储成功.";
    }

    @RequestMapping("/session1")
    public String getSession1(HttpServletRequest request) {
        HttpSession session = request.getSession(false);
        String username = null;
        if (session != null && session.getAttribute("username") != null) {
            username = (String) session.getAttribute("username");
        }
        return  "username: " + username;
    }

    @RequestMapping("/session2")
    public String getSession2(@SessionAttribute(value = "username", required = false) String username) {
        return "username: " + username;
    }

    @RequestMapping("/showBalance")
    public String renameParam(@RequestParam(value = "money", required = false) String balance){
         return "账户余额为: " + balance;
    }

    @RequestMapping("/login2/{username}/{password}")
    public String login2(@PathVariable String username, @PathVariable String password) {
        return "username: " + username + " | " + "password: " + password;
    }

    @RequestMapping("/userinfo1")
    public User getUserInfo1() {
        User user = new User();
        user.setId(1);
        user.setName("zhangsan");
        user.setPassword("123");
        user.setGender("male");
        user.setAge(20);
        return user;
    }

    @RequestMapping("/userinfo2")
    public Map<String, Object> getUserInfo2() {
        HashMap<String, Object> user = new HashMap<>();
        user.put("name", "zhangsan");
        user.put("password", "123");
        return user;
    }

    @RequestMapping("/userinfo3")
    public List<User> getUserInfo3() {
        List<User> userList = new ArrayList<>();
        User user1 = new User();
        user1.setName("zhangsan");
        User user2 = new User();
        user2.setName("lisi");
        userList.add(user1);
        userList.add(user2);
        return userList;
    }

    @RequestMapping(value = "/usertest")
    public void userTest(@RequestParam(value = "username1",required = true,defaultValue = "username01")  String username,
                         @RequestParam(value = "password1", required = false, defaultValue = "123456") String password ){
        System.out.println("username: " + username);
        System.out.println("password: " + password);
    }
}
